const Koa = require('koa');
const requireDirectory = require('require-directory');
const Router = require('koa-router');

const app = new Koa();

// console.log(module.filename); // 指向当前文件路径+文件名
// const modules = requireDirectory(module, './api/v1')
// console.log('modules:',modules)
/*
modules: {
  test1:
   Router {
     opts: {},
     methods:
      [ 'HEAD', 'OPTIONS', 'GET', 'PUT', 'PATCH', 'POST', 'DELETE' ],
     params: {},
     stack: [ [Layer] ] },
  test2:
   Router {
     opts: {},
     methods:
      [ 'HEAD', 'OPTIONS', 'GET', 'PUT', 'PATCH', 'POST', 'DELETE' ],
     params: {},
     stack: [ [Layer] ] } }

导出的是一个对象
key即是require的那个文件夹里的每一个文件的名字
value即是该文件导出的模块(module.exports)
*/

requireDirectory(module, './api/v1', {visit/*每当requireDirectory加载到一个文件时,都会调用该回调函数*/: whenLoadModule});

function whenLoadModule(m) {
  if (m instanceof Router) {

    app.use(m.routes())
  }
}

app.listen(3000, () => {
  console.log(`server is running at 3000`);
});
